
//
//  GoodsCollectionViewCell.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/7/27.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class GoodsCollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var img: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var nowPrice: UILabel!
    @IBOutlet weak var bazaarPriceLabel: UILabel!
    @IBOutlet weak var bgView: UIView!
    
    var loginClosure:TouchBlock!
    var model:SiteGoodsModel!
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.bgView.setShadow(color: "000000", offset: CGSize.init(width: 0, height: 1.5), radius: 3, opacity: 0.15)
        self.bgView.setCornerRadio(radio: 10, isMask: false)
        self.corner(byRoundingCorners: [.topLeft,.topRight], radii: 10, imgV: self.img)
    }
    
    
    func reload(withModel model:SiteGoodsModel){
        self.model = model
        self.img.my_setImage(url: model.img, placeholder: Api.placeHolder.rawValue)
        self.nameLabel.text = model.name
        self.nowPrice.attributedText = Utils.changeLabelTextFontSize(normalString: "¥\(model.price)/斤", changeString: "\(model.price)".getPriceInteger(), font: UIFont.systemFont(ofSize: 17))
        self.bazaarPriceLabel.attributedText = Utils.labelTextDeleLine(normalString: "市场价：￥\(model.marketPrice)", deleString: "￥" + model.marketPrice)
    }
    
    
    func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat,imgV:UIImageView) {
        let width = ((UIScreen.ScreenWidth - 30) * 0.5) - 10 //(UIScreen.ScreenWidth - 30)/2 - 10
        let height = width/(16/13)
        let bounds = CGRect.init(x: imgV.left, y: imgV.top, width: width, height: height)
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        imgV.layer.mask = maskLayer
    }
    
    @IBAction func addMethod(_ sender: UIButton) {
        guard Utils.isLogined() else{
            PKhud.showText(text: "请先登录~")
            if self.loginClosure != nil {
                self.loginClosure()
            }
            return
        }
        Utils.insertCart(goodsId: self.model.id) { (value) in
            if value.0 {
                PKhud.showText(text: "添加成功~")
            }else{
                PKhud.showText(text: value.1)
            }
        }
    }
}
